package com.example.taobao;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.taobao.data.userdata;
import com.example.taobao.databinding.MineFragmentBinding;
import com.example.taobao.ui.dashboard.DashboardViewModel;
import com.example.taobao.ui.login.LoginActivity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class mineFragment extends Fragment {

    private MineViewModel mViewModel;
    private MineFragmentBinding binding;
    private TextView textView1,textView2;

    private ImageButton imageButton;

    public static mineFragment newInstance() {
        return new mineFragment();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {





        View view = inflater.inflate(R.layout.mine_fragment,null);

        mViewModel =
                new ViewModelProvider(this).get(MineViewModel.class);

        binding = MineFragmentBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
//
//        textView1 = binding.userDisplayName;
//        textView2 = binding.userVipName;

        textView1 = view.findViewById(R.id.userDisplayName);
        textView2 = view.findViewById(R.id.userVipName);

        imageButton = view.findViewById(R.id.userImageBut);


        imageButton.setOnClickListener(onClickListener);

        Button loginbuttt = view.findViewById(R.id.loginbutt);
        Button exitButtt = view.findViewById(R.id.exitbutt);
        userdata userlogin = new userdata();


        final TextView textView = binding.textminefragment;


        loginbuttt.setOnClickListener(onClickListener);
        exitButtt.setOnClickListener(onClickListener);
//        Bundle bundle=getActivity().getIntent().getExtras();
//
//        if(bundle!=null){
//            String name = bundle.getString("name");
//            String username = bundle.getString("vipname");
//            textView1.setText(name);
//            textView2.setText(username);
//
//        }

        BmobQuery<userdata> bmobQuery = new BmobQuery<userdata>();
        bmobQuery.findObjects(new FindListener<userdata>() {
            @Override
            public void done(List<userdata> list, BmobException e) {
                if(e==null){
                    int i;
                    Boolean gname;
                    for (i=0;i<list.size();i++){
                        gname = list.get(i).getLoginZt();
                        if(gname){
                            textView1.setText(list.get(i).getVipName());
                            textView2.setText(list.get(i).getUsername());
                            Toast.makeText(getActivity(), "已登入 欢迎："+list.get(i).getVipName(), Toast.LENGTH_SHORT).show();

                            break;
                        }
                    }
                    if(i==list.size()){
                        Toast.makeText(getActivity(), "尚未登入", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


        mViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return view;
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent;
            Bundle bundle=getActivity().getIntent().getExtras();
            switch (view.getId()){
                case R.id.loginbutt:
                    intent = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(intent,0);
                    break;
                case R.id.exitbutt:
                    BmobQuery<userdata> bmobQuery = new BmobQuery<userdata>();
                    bmobQuery.findObjects(new FindListener<userdata>() {
                        @Override
                        public void done(List<userdata> list, BmobException e) {
                            if(e==null){
                                for (int i = 0;i<list.size();i++){
                                    if(list.get(i).getLoginZt()){
                                        String object = list.get(i).getObjectId();
                                        userdata p2 = new userdata();
                                        p2.setLoginZt(false);
                                        p2.update(object, new UpdateListener() {
                                            @Override
                                            public void done(BmobException e) {
                                                if(e==null){
                                                    Toast.makeText(getActivity(), "已退出", Toast.LENGTH_SHORT).show();
                                                }else{
                                                    Toast.makeText(getActivity(), "状态更新失败", Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                        });
                                    }
                                }
                            }else{
                                Toast.makeText(getActivity(), "连接失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    textView1.setText("");
                    textView2.setText("");
                    break;
                case R.id.userImageBut:
                    Intent intent1 = new Intent(getActivity(),DataMainActivity.class);
                    startActivityForResult(intent1,88);
                    break;


                default:
                    break;

            }
        }
    };


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(MineViewModel.class);
        // TODO: Use the ViewModel
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}